<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="query"
        @submit.native.prevent
        ref="queryForm"
        v-show="showSearch"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="数据名称" prop="dictLabel">
          <common-input
            v-model="query.dictLabel"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="字典类型" prop="dictName">
          <el-select v-model="query.dictName" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in optionDicArr"
              :key="item.dictId"
              :label="item.dictName"
              :value="item.dictName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->
    <el-row :gutter="10" class="mb8" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="edit"
          v-hasPermi="['system:dictdata:add']"
          >新增
        </el-button>
      </el-col>
    </el-row>
    <!--列表 start-->
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="数据名称"
        align="left"
        prop="dictLabel"
        show-overflow-tooltip
        width="400"
      >
        <template slot-scope="{ row }">{{ row.dictLabel }}</template>
      </el-table-column>

      <el-table-column
        label="字典类型"
        align="left"
        prop="dictName"
        width="200"
      >
        <template slot-scope="{ row }">{{ row.dictName }}</template>
      </el-table-column>

      <el-table-column
        label="内容"
        align="left"
        prop="dictValue"
        width="220"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.dictValue }}</template>
      </el-table-column>

      <el-table-column
        label="备注"
        align="left"
        prop="remark"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.remark }}</template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="95px">
        <template slot-scope="{ row }">
          <!-- <el-button type="text" @click="view(row.id)">详情</el-button> -->
          <el-button
            type="text"
            @click="edit(row)"
            v-hasPermi="['system:dictdata:edit']"
            >编辑</el-button
          >
          <el-button
            v-if="row.isDefault == 'N'"
            type="text"
            @click="handleDelete(row)"
            v-hasPermi="['system:dictdata:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
    <!-- 列表 end -->
    <editDialog
      ref="editDialog"
      :optionDicArr="optionDicArr"
      @refresh="getList"
    />
    <dataDetail ref="dataDetail" />
  </common-list>
</template>

<script>
import {
  dict_data_list,
  optionselect,
  delDictData,
} from "@/api/management/configure/index.js";
import editDialog from "./components/dataDialog";
import dataDetail from "./components/dataDetail";
import BaseList from "@/mixins/BaseList";

export default {
  mixins: [BaseList],
  components: { editDialog, dataDetail },
  props: {},
  data() {
    return {
      showSearch: true, // 查询部分显示
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      selectionIds: "", // 列表选中数据id
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      optionDicArr: [], // 字典类型
      query: {
        pageNum: 1,
        pageSize: 10,
        dictName: null,
      }, // 查询条件
    };
  },
  watch: {},
  computed: {},
  methods: {
    /**
     * 选择
     */
    handleSelectionChange(selection) {
      this.selectionIds = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /**
     * 获取列表
     */
    getList() {
      let query = { ...this.query };
      this.loading = true;
      dict_data_list(query)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    optionselect() {
      optionselect({}).then((res) => {
        if (res && res.code === 200) {
          this.optionDicArr = res.data;
        } else {
          this.optionDicArr = [];
        }
      });
    },
    /**
     * 查询
     */
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
    },
    /**
     * 添加/编辑
     */
    edit(row) {
      this.$refs.editDialog.show(row ? { ...row } : {});
    },
    /**
     * 查看详情
     */
    view(dictCode) {
      this.$refs.dataDetail.show(dictCode);
    },
    /** 字典删除按钮操作 */
    handleDelete(row) {
      // if (row) {
      //     let isDefault = row.isDefault;
      //     if (isDefault = "Y") {
      //
      //       return
      //     }
      // }
      const ids = row.id ? row.id : this.ids.join(",");
      this.$GlobalDeleteConfirm()
        .then(() => {
          delDictData({ ids }).then((res) => {
            if (res && res.code == 200) {
              this.$GlobalSuccessMessage("删除成功");
              this.getList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg).then();
            }
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.getList();
    this.optionselect();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
